{% extends '../_manage.html' %}

{% block title %} Edit Wiki Page {% endblock %}

{% block head %}
<script>
$(function () {
    var id = '{{ id }}';
    if (id) {
        getJSON('/api/wikis/wikipages/' + id, function (err, wp) {
            if (err) {
                return showError(err);
            }
            initVM(wp);
        });
    }
    else {
        initVM(wp);
    }
});

function initVM(wp) {
    var vm = new Vue({
        el: '#vm',
        data: {
            name: wp.name,
            content: wp.content || ''
        },
        methods: {
            getPostData: function () {
                var data = {
                    name: this.name
                }
                var content = $('#content-editor').val();
                if (this.content !== content) {
                    data.content = content;
                }
                return data;
            },
            submit: function(event) {
                event.preventDefault();
                var
                    data,
                    $form = $('#vm').find('form');
                try {
                    data = this.getPostData();
                }
                catch (e) {
                    $form.showFormError(e);
                    return;
                }
                $form.postJSON('{{ form.action }}', data, function(err, r) {
                    if (!err) {
                        window.onbeforeunload = null;
                        location.assign('{{ form.redirect }}');
                    }
                });
            }
        }
    });

    $('#loading').hide();
    $('#vm').show();

    // check content when unload:
    window.onbeforeunload = function() {
        if (vm.content !== $('#content-editor').val()) {
            return 'Content has been changed.';
        }
    };

    $('#content-editor').val(wp.content || '');
    var htmleditor = UIkit.htmleditor($('#content-editor').get(0), {
        markdown: true,
        maxsplitsize: 600
    });
    g=vm;
}
</script>

{% endblock %}

{% block main %}

    <div id="loading" class="uk-width-1-1">
        <i class="uk-icon-spinner uk-icon-spin"></i> {{ _('Loading') }}...
    </div>

    <div id="vm" class="uk-width-1-1">
        <ul class="uk-breadcrumb">
            <li><a href="wiki_list">{{ _('All Wikis') }}</a></li>
            <li class="uk-active"><span>{{ form.name }}</span></li>
        </ul>

        <form v-on:submit.prevent="submit" class="uk-form uk-form-stacked uk-margin">
            <legend>{{ form.name }}</legend>
            <fieldset>
                <div class="uk-alert uk-alert-danger uk-hidden"></div>
                <div class="uk-form-row">
                    <label class="uk-form-label">{{ _('Name') }}:</label>
                    <div class="uk-form-controls">
                        <input v-model="name" type="text" maxlength="100" class="uk-width-1-1" placeholder="name">
                    </div>
                </div>
                <div class="uk-form-row">
                    <label class="control-label">Content:</label>
                    <div class="controls">
                        <textarea id="content-editor" rows="20" style="width:100%; resize:none;"></textarea>
                    </div>
                </div>
                <div class="uk-form-row">
                    <div class="controls">
                        <button type="submit" class="uk-button uk-button-primary"><i class="uk-icon-check"></i> {{ _('Save') }}</button>
                        <button type="button" class="uk-button" onclick="location.assign('{{ form.redirect }}')"><i class="uk-icon-times"></i> {{ _('Cancel') }}</button>
                    </div>
                </div>
            </fieldset>
        </form>
    </div>

{% endblock %}
